/**
 * 
 */
package com.avocado.base.entity;


import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.avocado.commons.utils.JsonDateSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;

/**
 *
 * @author wenfeng.xu
 *2011-4-24下午08:35:40
 */

@Entity
@Table(name="AVO_ROLE")
@JsonAutoDetect
@JsonIgnoreProperties(value={"userRoles","resourcesRoles"})
@Data
public class Role {
	
	
	@Id@Column(name="ID",nullable=false)
	@GeneratedValue(strategy = GenerationType.TABLE,generator="id_gen")
    @TableGenerator(name = "id_gen",pkColumnValue="ROLE_ID",table="AVO_ID_GENERATOR", pkColumnName="GEN_NAME",valueColumnName="GEN_VALUE",allocationSize=1)
	private Long id;
	
	@Column(name="NAME",nullable=false)
	private String name;
	
	@Column(name="CODE",nullable=false,unique=true)
	private String code;
	
	@Column(name="DESCRIPTION")
	private String description="";
	
	@Column(name="CATEGORY")
	private String category;
    
    @Temporal(TemporalType.TIMESTAMP)@Column(nullable=false,name="CREATE_TIME")
	private Date createTime=new Date();
    
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="role")
	private Set<UserRole> userRoles=new HashSet<UserRole>();
	
	@OneToMany(cascade=CascadeType.REFRESH, fetch=FetchType.EAGER, mappedBy="role")
	private Set<ResourcesRole> resourcesRoles =new HashSet<ResourcesRole>();
	@JsonSerialize(using= JsonDateSerializer.class)
	public Date getCreateTime() {
		return createTime;
	}

	public Long getId() {
		return id;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Role role = (Role) o;
		return Objects.equals(id, role.id);
	}

	@Override
	public int hashCode() {
		return Objects.hash(id);
	}
}
